<?php

class Hocvien extends MY_Admin
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();

		// Check if the logged user is an administrator
		//$this->access_library->check_access();
		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'hocvien_model', 'hocvien');
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'hocvien');
		$this->user = $this->access->get_user();
		$this->load->helper('url'); 
		$this->load->library('basic_library');
		//You should autoload this one ;)
		//$this->access->restrict('Admin.Hocvien.Index');
		//$this->access->restrict();
	}

	// Public methods
	public function index() 
	{
		$this->access->restrict('Admin.Hocvien.Index');
		
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Học viên';		
		
		
		$this->load->library('pagination');
		$this->load->helper('url');
		$config = array();
		$config['base_url'] 	= base_url().'admin/hocvien/';
		$config['total_rows'] 	= $this->hocvien->count_all_hocvien();
		$config['per_page'] 	= 100;
		$config['uri_segment']	= 3;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu tiên';
        $config['last_link'] = 'Cuối cùng';
        $config['next_link'] = 'Tiếp theo';
        $config['prev_link'] = 'Lùi lại';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		
		$data['hocviens'] 	= $this->hocvien->get_all_hocvien($config['per_page'], $page);
		$data['lops'] 		= $this->hocvien->get_tenlop();
		$data['links'] 		= $this->pagination->create_links();		
		
		$this->_template['page']	= 'hocvien/list';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	function date_check($date) 
	{
    	$ddmmyyy='(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)[0-9]{2}';
    	if(preg_match("/$ddmmyyy$/", $date))
    	{
      		return TRUE;
    	} else
    	{
     		$this->form_validation->set_message('date_check', 'Nhập định dạng ngày tháng dd/mm/yyyy');
     	return FALSE;
    	}
	}  
	
	public function search()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Tìm kiếm thông tin';
		$data['search_keyword'] = trim($this->input->post('keyword', TRUE));
		if($data['search_keyword']!=='')
		{
			$data['result'] = $this->hocvien->search_data($data['search_keyword']);
			
				$this->_template['page']	= 'hocvien/result';
				$this->system_library->load($this->_template['page'], $data, TRUE);
			
			
		}
		else
		{
			redirect('admin/hocvien', 'refresh');
		}
	}
	
	public function add()
	{
		$this->access->restrict('Admin.Hocvien.Add');
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm mới sinh viên';
		$data['description'] = 'Thêm mới sinh viên';

		$this->load->helper('form');
		$data['lops'] = $this->hocvien->get_tenlop();
		$userid = $this->user->id;
		$lang_working = load_language_working();
		
		//here is validation form
		$this->form_validation->set_rules('mahv', 'lang:form_mahv', 'required|max_length[50]|xss_clean|callback_check_mahv');
		$this->form_validation->set_rules('tenhv', 'lang:form_tenhv', 'required');
		$this->form_validation->set_rules('email', 'lang:form_email', 'required|xss_clean|trim|valid_email');
		$this->form_validation->set_rules('nguyenquan','lang:form_nguyenquan','required|max_length[255]');
		$this->form_validation->set_rules('ngaysinh','lang:form_ngaysinh','required|callback_date_check');
		$this->form_validation->set_rules('thuongtru','lang:form_thuongtru','max_length[255]');
		$this->form_validation->set_rules('didong','lang:form_didong','max_length[15]');
		$this->form_validation->set_rules('cmnd','lang:form_cmnd','numeric|callback_check_cmnd');
		$this->form_validation->set_rules('ngaycap','lang:form_ngaycap','required');
		if($this->input->post('bodoi')=='1')
		{
			$this->form_validation->set_rules('sothexuatngu','lang:form_sothexuatngu','required');
			$this->form_validation->set_rules('ngayxuatngu','lang:form_ngayxuatngu','required');
		}
		else
		{
			$this->form_validation->set_rules('sothexuatngu','lang:form_sothexuatngu','');
			$this->form_validation->set_rules('ngayxuatngu','lang:form_ngayxuatngu','');
		}
		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{		
			$this->hocvien->add_hocvien();
			$this->session->set_flashdata('message', lang('successfully_created'));	
			redirect('admin/hocvien/', 'refresh');
		}	
		
		$this->_template['page']	= 'hocvien/add';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	//Edit product
	public function edit($id)
	{
		$this->access->restrict('Admin.Hocvien.Edit');
		//metadata
		$data['ItemID']		= 'User';
		$data['title']		= "Sửa thông tin Học viên"; 
		$data['info'] 		= $this->hocvien->get_hocvien_info($id);
		$data['lops'] 		= $this->hocvien->get_tenlop();
		
		
		if($data['info']->mahv==$this->input->post('mahv'))
		{
			$this->form_validation->set_rules('mahv', 'lang:form_mahv', 'required|max_length[50]|xss_clean');
		}
		else
		{
			$this->form_validation->set_rules('mahv', 'lang:form_mahv', 'required|max_length[15]|xss_clean|callback_check_mahv');
		}
		$this->form_validation->set_rules('tenhv', 'lang:form_tenhv', 'required');
		if($this->input->post('email') == '' || $data['info']->email==$this->input->post('email'))
		{
			$this->form_validation->set_rules('email', 'lang:form_email', 'xss_clean');
		}
		else
		{
			$this->form_validation->set_rules('email', 'lang:form_email', 'required|xss_clean|trim|valid_email');
		}
		$this->form_validation->set_rules('nguyenquan','lang:form_nguyenquan','max_length[255]');
		$this->form_validation->set_rules('thuongtru','lang:form_thuongtru','max_length[255]');
		$this->form_validation->set_rules('didong','lang:form_didong','max_length[15]');
		
		if($this->input->post('bodoi')=='1')
		{
			$this->form_validation->set_rules('sothexuatngu','lang:form_sothexuatngu','required');
			$this->form_validation->set_rules('ngayxuatngu','lang:form_ngayxuatngu','required');
		}
		
		if($data['info']->cmnd == $this->input->post('cmnd'))
		{
			$this->form_validation->set_rules('cmnd','lang:form_cmnd','');
		}
		else
		{
			$this->form_validation->set_rules('cmnd','lang:form_cmnd','required|numeric|callback_check_cmnd');
		}
					
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			$this->hocvien->edit_hocvien($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));	
			redirect('admin/hocvien/', 'refresh');	
		}	
		$this->_template['page']	= 'hocvien/edit';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function check_mahv()
	{
        if($this->hocvien->check_mahv())
        {
            $this->form_validation->set_message('check_mahv','MAHV đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }
	public function check_cmnd()
	{
        if($this->hocvien->check_cmnd())
        {
            $this->form_validation->set_message('check_cmnd','CMND đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }
    
	
	public function delete($id)
	{
		$this->access->restrict('Admin.Hocvien.Delete');
		$this->hocvien->delete_hocvien($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));
		redirect('admin/hocvien', 'refresh');
	}
	
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->hocvien->change_status_hocvien($id,1);
		redirect('admin/hocvien', 'refresh');
		
	}
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->hocvien->change_status_hocvien($id,0);
		redirect('admin/hocvien', 'refresh');
	}
}

/* End of file jobs.php */